﻿using RXY_Mall.Application;
using RXY_Mall.Application.Goods;
using RXY_Mall.Application.Marketing;
using RXY_Mall.Application.Marketing.Dto;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class MarketingController : BaseController
    {
        private IMarketingService marketingService;
        private IGoodsService _goodsService;

        public MarketingController()
        {
            marketingService = new MarketingService();
            _goodsService = new GoodsService();
        }

        [HttpGet]
        public ActionResult GetGoods()
        {
            return View();
        }

        public JsonResult GoodsList(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string title, string shopName, string code, string category, int status, int shopId = -1, int categoryId = -1, int type = 1)
        {
            int totalCount = 0;
            var list = _goodsService.GetAllGoodsByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, title, shopName, code, category, categoryId, shopId, status, type, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }

        #region Banners
        [HttpGet]
        public ActionResult Banners()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetBanners(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, int status = -1)
        {
            var result = marketingService.GetBanners(start_date, end_date, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);

            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用
        [HttpPost]
        public async Task<ActionResult> UpdateBannerStatus(UpdateBannerStatusInput input)
        {
            var result = await marketingService.UpdateBannerStatus(input);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(result);
        }

        [HttpGet]
        public async Task<ActionResult> Banner(int id = 0)
        {
            GetBannerOutput output = new GetBannerOutput();
            if (id == 0)
            {
                ViewBag.text = "添加";
            }
            else
            {
                ViewBag.text = "修改";
                var result = await marketingService.GetBanner(id);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    output = result.data;
                }
            }
            return View(output);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Banner(GetBannerOutput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.UpdateBanner(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("Banners");
                }
            }
            return View(input);
        }

        //[HttpDelete]
        //public JsonResult DeleteBanner(int id)
        //{
        //    var result = marketingService.DeleteBanner(id);
        //    if (result.code != 1)
        //    {

        //    }
        //    return Json();
        //}

        #endregion

        #region 首页商品
        [HttpGet]
        public ActionResult HomeGoods()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetHomeGoods(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, int status = -1)
        {
            var result = marketingService.GetHomeGoods(start_date, end_date, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用
        [HttpPost]
        public async Task<ActionResult> UpdateHomeGoodsStatus(UpdateHomeGoodsStatusInput input)
        {
            var result = await marketingService.UpdateHomeGoodsStatus(input);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(result);
        }

        [HttpGet]
        public async Task<ActionResult> HomeGoodsDetail(int id = 0)
        {
            GetHomeGoodsOutput output = new GetHomeGoodsOutput();
            if (id == 0)
            {
                ViewBag.text = "添加";

            }
            else
            {
                ViewBag.text = "修改";
                var result = await marketingService.GetHomeGoods(id);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    output = result.data;
                }
            }
            return View(output);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> HomeGoodsDetail(GetHomeGoodsOutput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.UpdateHomeGoods(input);
                if (result.code == 1)
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("HomeGoods");
                }
                else
                {
                    ErrorNotification(result.msg);
                }
            }
            return View(input);
        }


        [HttpGet]
        public ActionResult HomeGoodsBatchAdd()
        {
            BatchAddHomeGoodsInput input = new BatchAddHomeGoodsInput();
            return View(input);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> HomeGoodsBatchAdd(BatchAddHomeGoodsInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.BatchAddHomeGood(input);
                if (result.code == 1)
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("HomeGoods");
                }
                else
                {
                    ErrorNotification(result.msg);
                }
            }
            return View(input);
        }


        #endregion

        #region 优惠券
        [HttpGet]
        public ActionResult Coupons()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetCoupons(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, int status = -1)
        {
            var result = marketingService.GetCoupons(start_date, end_date, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用
        [HttpPost]
        public async Task<ActionResult> UpdateCouponStatus(UpdateCouponStatusInput input)
        {
            var result = await marketingService.UpdateCouponStatus(input);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(result);
        }

        [HttpGet]
        public async Task<ActionResult> Coupon(int id = 0)
        {
            GetCouponOutput output = new GetCouponOutput();
            if (id == 0)
            {
                ViewBag.text = "添加";
            }
            else
            {
                ViewBag.text = "修改";
                var result = await marketingService.GetCoupon(id);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    output = result.data;
                }
            }
            return View(output);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Coupon(GetCouponOutput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.UpdateCoupon(input);
                if (result.code == 1)
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("Coupons");
                }
                else
                {
                    ErrorNotification(result.msg);
                }
            }
            return View(input);
        }

        [HttpGet]
        public ActionResult CouponBatchAdd()
        {
            BatchAddCouponInput input = new BatchAddCouponInput();
            return View(input);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CouponBatchAdd(BatchAddCouponInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.BatchAddCoupon(input);
                if (result.code == 1)
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("Coupons");
                }
                else
                {
                    ErrorNotification(result.msg);
                }
            }
            return View(input);
        }
        #endregion

        #region 补贴商品
        [HttpGet]
        public ActionResult Subsidies()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetSubsidies(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, int status = -1)
        {
            var result = marketingService.GetSubsidies(start_date, end_date, status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        //禁用/启用
        [HttpPost]
        public async Task<ActionResult> UpdateSubsidyStatus(UpdateSubsidyStatusInput input)
        {
            var result = await marketingService.UpdateSubsidyStatus(input);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(result);
        }

        [HttpGet]
        public async Task<ActionResult> Subsidy(int id = 0)
        {
            AddSubsidyInput output = new AddSubsidyInput();
            if (id == 0)
            {
                ViewBag.text = "添加";
            }
            else
            {
                ViewBag.text = "详细";
                var result = await marketingService.GetSubsidy(id);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                    return RedirectToAction("Subsidies");
                }
                else
                {
                    output = result.data;
                }
            }
            return View(output);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Subsidy(AddSubsidyInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await marketingService.AddSubsidy(input);
                if (result.code == 1)
                {
                    SuccessNotification(result.msg);
                    return RedirectToAction("Subsidies");
                }
                else
                {
                    ErrorNotification(result.msg);
                }
            }
            return View(input);
        }

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> Subsidy(GetSubsidyOutput input)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var result = await marketingService.UpdateSubsidy(input);
        //        if (result.code == 1)
        //        {
        //            SuccessNotification(result.msg);
        //            return RedirectToAction("Subsidies");
        //        }
        //        else
        //        {
        //            ErrorNotification(result.msg);
        //        }
        //    }
        //    return View(input);
        //}
                
        #endregion
    }
}